/**
 * 
 */
package co.edu.javeriana.ia.ga;

import java.util.Collection;
import java.util.List;

import org.jgap.Gene;
import org.jgap.IChromosome;

/**
 * <p>
 * Funcion de utilidad de impresion
 * </p>
 * 
 * @author <a href=f.triana@javeriana.edu.co">Alejandro Triana</a>
 * @version 1.0
 */
public class PrintUtil {

	/**
	 * <p>
	 * Imprime el valor de la funcion de Fitness
	 * </p>
	 */
	public static void printFitnessResult(IChromosome chr, int sequence,
			double[] totalResults, double eval) {
		System.out
				.printf("Number of test sequences:%d\n"
						+ "Non-classified:%f\tWell classified:%f\tUnwell classified:%f\n"
						+ "Descriptors match:%f\n" + "Fitness:%f\n", sequence,
						totalResults[0], totalResults[1], totalResults[2],
						totalResults[3], eval);
	}

	/**
	 * <p>
	 * Permite imprimir los valores para un respectivo cromosoma
	 * </p>
	 */
	public static void printChromosome(IChromosome chr) {

		Gene[] genes = chr.getGenes();

		int size = chr.getUniqueID().length();
		System.out.printf("Chromosome:%s\n",
				chr.getUniqueID().substring(size - 4, size));

		for (Gene gene : genes) {

			// System.out.printf("Gene %d\n", index++);

			RuleGene rule = (RuleGene) gene;
			RuleAllele ruleAllele = (RuleAllele) rule.getAllele();

			Collection<Descriptor> conditionDescriptor = ruleAllele
					.getCondition().values();

			System.out.printf("Rule:%s\t", ruleAllele.getAction().getName());

			for (Descriptor d : conditionDescriptor) {
				System.out.printf("%s:%d\t", d.getId(), d.getFrecuency());
			}

			System.out.printf("->\t%d:%s", ruleAllele.getAction()
					.getInternalId(), ruleAllele.getAction().getName());
			System.out.printf("\n");
		}
	}

	/**
	 * <p>
	 * Permite imprimir los valores para un respectivo cromosoma
	 * </p>
	 */
	public static void printFitnness(IChromosome chr) {
		System.out.printf("%s\tFitness: %f \n", chr.getUniqueID(),
				chr.getFitnessValue());
	}

	/**
	 * <p>
	 * Imprime el fittnes cuando se prueba la secuencia
	 * </p>
	 */
	public static void printBestFitnness(IChromosome chr) {
		System.out.printf("%s\tTestFitness: %f \n", chr.getUniqueID(),
				chr.getFitnessValueDirectly());
	}

	/**
	 * <p>
	 * Imprime la iteracion
	 * </p>
	 */
	public static void printIteration(int ite) {
		System.out.printf("\nIteration: %d\n\n", ite);
	}

	/**
	 * <p>
	 * Imprime los avisos cuando se inician las secuencias pruebas
	 * </p>
	 */
	public static void printTestExecution() {
		System.out.printf("\n\nDoing testing with testing sequences.\n\n");
	}

	/**
	 * <p>
	 * Imprime un string
	 * </p>
	 */
	public static void printString(String s) {
		System.out.printf("%s\n", s);
	}

	/**
	 * <p>
	 * Imprime la poblacion
	 * </p>
	 */
	public static void printPopulation(List<IChromosome> lstChr) {
		for (IChromosome iChromosome : lstChr) {
			PrintUtil.printString("\nChr" + iChromosome.getUniqueID());
		}
	}
}
